package sfeir.ergosum.adapter;

import java.util.ArrayList;

import sfeir.ergosum.R;
import sfeir.ergosum.model.Address;
import sfeir.ergosum.model.Person;
import sfeir.ergosum.tools.ThemeColor;
import android.content.Context;
import android.content.Intent;
import android.graphics.PorterDuff.Mode;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AddressAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<Address> details;
    private Person p;
    private ViewHolder holder;

    public AddressAdapter(Context c, Person p) {
        mContext = c;
        this.p = p;
        mInflater = LayoutInflater.from(mContext);
        details = new ArrayList<Address>();
        populate();
    }

    public void update(Person newP) {
        p = newP;
        details.clear();
        populate();
        notifyDataSetChanged();
    }

    private void populate() {
        for (Address t : p.getAddresses())
            details.add(t);
    }

    private static class ViewHolder {
        ImageView logo;
        TextView info;
    }

    @Override
    public int getCount() {
        return details.size();
    }

    @Override
    public Address getItem(int position) {
        return details.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.logo = (ImageView) convertView.findViewById(R.id.list_item_image);
            holder.info = (TextView) convertView.findViewById(R.id.list_item_content);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Address a = getItem(position);
        final String addr = a.getFullValue();
        if (addr != null && !addr.equalsIgnoreCase("")) {
            OnClickListener addrListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + addr));
                    mContext.startActivity(i);
                }
            };

            holder.info.setText(addr);
            convertView.setClickable(true);
            convertView.setOnClickListener(addrListener);

            switch (a.getType()) {

            case Address.PERSONAL_ADDRESS:
                holder.logo.setImageResource(R.drawable.house);
                break;
            case Address.PROFESSIONNAL_ADDRESS:
                holder.logo.setImageResource(R.drawable.work);
                break;
            case Address.DEFAULT_ADDRESS:
                holder.logo.setImageResource(R.drawable.house);
                break;
            default:
                holder.logo.setImageResource(R.drawable.house);
                break;
            }
        }        
        ThemeColor theme = new ThemeColor(mContext);
        convertView.getBackground().setColorFilter(theme.getCellColors().get(1), Mode.SRC);
        convertView.getBackground().setAlpha(200);
        return convertView;
    }
}
